﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using GEPSDevelopEnvironment.Common;
using Microsoft.Win32;

namespace GEPSDevelopEnvironment.Common
{
    class TortoiseSVNClientAdapter
    {
        public static void ExecTortoiseSVN(emSVNOperater AOperater, string APath, string AParameter = "")
        {
            switch (AOperater)
            {
                case emSVNOperater.svnAdd:
                    Add(APath);
                    break;
                case emSVNOperater.svnCommit:
                    Commit(APath, AParameter);
                    break;
                case emSVNOperater.svnDelete:
                    Delete(APath);
                    break;
                case emSVNOperater.svnDiff:
                    Diff(APath);
                    break;
                case emSVNOperater.svnLock:
                    Lock(APath, AParameter);
                    break;
                case emSVNOperater.svnUpdate:
                    Update(APath);
                    break;
                case emSVNOperater.svnRevert:
                    Revert(APath);
                    break;
                case emSVNOperater.svnShowLog:
                    ShowLog(APath);
                    break;
                case emSVNOperater.svnUnlock:
                    UnLock(APath);
                    break;
            }
        }

        public static bool Add(string PathName)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:add /path:{0}", PathName));
        }

        public static bool Cleanup(string PathName)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:cleanup /path:{0}", PathName));
        }

        public static bool Commit(string PathName, string LogMessage)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:commit /path:{0} /logmsg:{1}", PathName, LogMessage));
        }

        public static bool Delete(string PathName)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:remove /path:{0}", PathName));
        }

        public static bool Diff(string PathName)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:diff /path:{0}", PathName));
        }

        public static string DosQuotedStr(string S)
        {
            return "\"" + S + "\"";
        }

        ////////////////////////////////////////////////////////////////////////////////
        //设计：Linc 2008.10.05
        //功能：执行命令
        //参数：
        //注意：字符串会自动添加 “”
        ////////////////////////////////////////////////////////////////////////////////
        public static bool ExecuteCommand(string AFmt)
        {
            string sTortoisePath = InitTortoise();
            if (!File.Exists(sTortoisePath))
            {
                //MessageBox.Show("没有安装TortoiseSVN！");
                return false;
            }
            int nExitCode;
            string strRet = ProcessCommonUtils.GetCommandLineOutput(AFmt, out nExitCode, sTortoisePath);
            if (strRet == "")
            {
                return true;
            }
            else
            {
                //MessageBox.Show(strRet);
                return false;
            }
        }

        public static bool Lock(string PathName, string LogMessage)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:lock /path:{0} /logmsg:{0}", PathName, LogMessage));
        }

        public static string InitTortoise()
        {
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey TortoiseSVN = hkml.OpenSubKey("SOFTWARE").OpenSubKey("TortoiseSVN");
            if (TortoiseSVN == null)
                return "";

            return TortoiseSVN.GetValue("ProcPath").ToString();
        }

        public static bool IsVersioned(string PathName)
        {
            return true;
        }

        public static bool Revert(string PathName)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:revert /path:{0}", PathName));
        }

        public static bool ShowLog(string PathName)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:log /path:{0}", PathName));
        }

        public static bool UnLock(string PathName)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:unlock /path:{0}", PathName));
        }

        public static bool Update(string PathName)
        {
            return ExecuteCommand(string.Format("/closeonend /notempfile /command:update /path:{0}", PathName));
        }

        private static string BrowsePath;
        private static bool bExit;
        private static void GetBrowsePath()
        {
            List<Process> oProcesses = new List<Process>();
            Process oProcess = null;
            while (!bExit)
            {
                Thread.Sleep(100);
                foreach (Process oValue in Process.GetProcesses())
                {
                    if (oValue.ProcessName.Contains("TortoiseProc"))
                    {
                        if (ProcessCommonUtils.GetParantPID(oValue.Id) == Process.GetCurrentProcess().Id)
                        {
                            oProcess = oValue;
                            break;
                        }
                    }
                }
                if (oProcess != null)
                {
                    IntPtr ptrWindow = Win32_API.FindWindowEx(oProcess.MainWindowHandle, IntPtr.Zero, "static", "");
                    if (ptrWindow != IntPtr.Zero)
                    {
                        ptrWindow = Win32_API.FindWindowEx(ptrWindow, IntPtr.Zero, "ReBarWindow32", "");
                        if (ptrWindow != IntPtr.Zero)
                        {
                            ptrWindow = Win32_API.FindWindowEx(ptrWindow, IntPtr.Zero, "ComboBoxEx32", "");
                            if (ptrWindow != null)
                            {
                                BrowsePath = Win32_API.GetWindowTextRaw(ptrWindow);
                            }
                        }
                    }
                }
            }
            
        }

        public static string Browse(string PathName)
        {
            Thread oThread = new Thread(GetBrowsePath);
            bExit = false;
            oThread.Start();
            if (PathName != "")
            {
                ExecuteCommand(string.Format("/closeonend /notempfile /command:repobrowser /path:{0}", PathName));
            }
            else
            {
                ExecuteCommand("/closeonend /notempfile /command:repobrowser");
            }
            bExit=true;
            Thread.Sleep(100);
            return BrowsePath;
        }

    }

}
